package com.tencent.qcloud.qiaoqiao.ui.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.example.zhouwei.library.CustomPopWindow;
import com.tencent.qcloud.qiaoqiao.R;
import com.tencent.qcloud.qiaoqiao.login.TCLoginActivity;
import com.tencent.qcloud.qiaoqiao.login.TCUserMgr;
import com.tencent.qcloud.qiaoqiao.mainui.list.TCVideoInfo;
import com.tencent.qcloud.qiaoqiao.ui.activity.VideoLoginActivity;
import com.tencent.qcloud.qiaoqiao.ui.net.HttpRequest;
import com.tencent.qcloud.qiaoqiao.ui.net.HttpResultListener;
import com.tencent.qcloud.qiaoqiao.ui.tools.utilCode.ActivityUtils;
import com.tencent.qcloud.qiaoqiao.ui.tools.utilCode.ScreenUtils;
import com.tencent.qcloud.qiaoqiao.ui.tools.utilCode.ToastUtils;

import okhttp3.FormBody;
import okhttp3.RequestBody;

public class SharePopupWindow extends PopupWindow implements View.OnClickListener{

    TextView share_collect_btn;
    private Context mContext;
    CustomPopWindow popWindow;

    public SharePopupWindow(Context context, View.OnClickListener listener, View view) {
        mContext = context;
        View contentView = LayoutInflater.from(context).inflate(R.layout.window_share_layout, null);
        //创建并显示popWindow
        int w = ScreenUtils.getScreenWidth();
        popWindow = new CustomPopWindow.PopupWindowBuilder(context)
                .setView(contentView).size(w, RelativeLayout.LayoutParams.MATCH_PARENT)
                .create().showAsDropDown(view);
        share_collect_btn = (TextView) contentView.findViewById(R.id.share_collect_btn);
        share_collect_btn.setOnClickListener(this);
        contentView.findViewById(R.id.share_close_btn).setOnClickListener(this);
        contentView.findViewById(R.id.share_top_close).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.share_collect_btn:
                loadCollectionData();
                break;
            case R.id.share_close_btn:
                popWindow.dissmiss();
                break;
            case R.id.share_top_close:
                popWindow.dissmiss();
                break;
        }
    }

    TCVideoInfo currentTcLiveInfo;

    public void setData(TCVideoInfo currentTcLiveInfo){
        this.currentTcLiveInfo = currentTcLiveInfo;
        int action = this.currentTcLiveInfo.collection_state;
        Drawable drawable = null;
        if (action == 1) {
            drawable = ContextCompat.getDrawable(mContext, R.mipmap.share_collect_icon2);
        } else {
            drawable = ContextCompat.getDrawable(mContext, R.mipmap.share_collect_icon);
        }
        drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
        share_collect_btn.setCompoundDrawables(null, drawable, null, null);
    }

    /**
     * 收藏
     */
    private void loadCollectionData() {
        if (TCUserMgr.getInstance().hasUser()){
            ActivityUtils.startActivity(VideoLoginActivity.class);
            return;
        }
        RequestBody formBody = new FormBody.Builder()
                .add("userid", TCUserMgr.getInstance().getUserId())
                .add("file_id", currentTcLiveInfo.fileid).build();
        HttpRequest.requestPost("collection", formBody, new HttpResultListener() {
            @Override
            public void onSuccess(String data) {
                JSONObject jsonObject = JSONObject.parseObject(data);
                int action = jsonObject.getInteger("action");
                Drawable drawable = null;
                if (action == 0) {
                    currentTcLiveInfo.collection_state = 1;
                    ToastUtils.showShort("收藏成功");
                    drawable = ContextCompat.getDrawable(mContext, R.mipmap.share_collect_icon2);
                } else {
                    currentTcLiveInfo.collection_state = 0;
                    ToastUtils.showShort("取消收藏成功");
                    drawable = ContextCompat.getDrawable(mContext, R.mipmap.share_collect_icon);
                }
                drawable.setBounds(0, 0, drawable.getMinimumWidth(), drawable.getMinimumHeight());
                share_collect_btn.setCompoundDrawables(null, drawable, null, null);
                popWindow.dissmiss();
            }

            @Override
            public void onFailure(String data) {
                ToastUtils.showLong("收藏失败");
            }
        });
    }

}